function note_view(id) {
    this.id = id;
    this.div = '#'+id+'_note-view';
    this.content = this.div 
    this.blocks = {};
    this.title = null;
    this.modified_block_ids = {}; 
    this.init = function () {
        this.blocks = {};
        //note_view.bind_add_block_btn_click();
    };

    /* open a note in background or not
     * */
    this.open = function (background) {
        var _this = this;
        get_note_do(this.id,
        function (state, data) {
            var note = data;
            _this.title = note.title;
            ubi.tabs.add(global.open_notes[_this.id].div, _this.title);
            _this.update_block_list();
            if (background != true)
                ubi.tabs.select(_this.div);        
        },
        function (state, data) {
            notify('open note failed.')
        })
    };

    /* reload all blocks
     * */
    this.reload = function () {
         this.update_block_list();
    }

    /* locate anchor
     * */
    this.locate_anchor = function (block_id) {
        //workaround for anchor location.
        var do_locate_anchor = function () {
            window.location.hash = block_id + '_block-item';
            //FIXED: Issue 3 http://code.google.com/p/ubinote/issues/detail?id=3
            //reset URI hash to empty. 
            setTimeout(function () {
                window.location.hash = '';
            }, 200);
        }
        window.setTimeout(do_locate_anchor, 500);
    };

    /*  
     * */
    this.bind_add_block_btn_click = function (){
        var _this = this;
        $('#block-new-btn').click(function () {
            _this.add_block('');
        });
    };
    
    this.fold_select = function () {
        var _this = this;
        $.each(_this.blocks, function(id, block){
            if (block.is_select)
                block.fold();
        }); 
        this.save_block_states();
    }

    this.unfold_select = function () {
        var _this = this;
        $.each(_this.blocks, function(id, block){
            if (block.is_select)
                block.unfold();
        }); 
        this.save_block_states();
    }

    this.remove_select = function () {
        var _this = this;
        $.each(_this.blocks, function(id, block){
            if (block.is_select)
                block.remove();
        }); 
    }

    this.select_all = function () {
        var _this = this;
        $.each(_this.blocks, function(id, block){
            block.select();
        }); 
    }

    this.disselect_all = function () {
        var _this = this;
        $.each(_this.blocks, function(id, block){
            block.disselect();
        }); 
    }

    this.invert_selection = function () {
        var _this = this;
        $.each(_this.blocks, function(id, block){
            block.toggle_select();
        }); 
    }
    this.add_block = function (str) {
        this.save_status();
        var _this = this;
        add_block_do (global.crr_note_id, str,
        function (state, data){
            var b = data;
            var block = new ubi.block(b);
            $(_this.content).find('ul').prepend(
                block.html()
            );
            block.bind_events();
            block.update_tags();
            _this.blocks[block.id] = block;
            _this.modified_block_ids[block.id] = 1; 
            //actions
            _this.block_items_dragsort_bind();
            notify('Add block ...done');    
        },
        function (state, data){
            notify('Add block  failed!.')
        });
    };

    this.save_status = function () {
        this.save_block_pos();
        if (this.modified_block_ids != {}) {
            this.save_block_content();
        }
    };

    this.save_block_content = function () {
        var _this = this;
        /*
        $(this.content).find('.block-item-content')
        .each(function (i, elem) {
            var id = $(this).attr('id').split('_')[0];
            if (id in _this.modified_block_ids) {
                var content = $(this).html();
                save_block_content_do(id, content,
                    function (state, data) {
                        _this.modified_block_ids = {};
                        result = true;
                    },
                    function (state, data) {
                        result = false;
                    });
            }
        });*/
        for (var id in this.modified_block_ids) {
            var content = $('#'+id + '_block-item-content').html();
            save_block_content_do(id, content,
                function (state, data) {
                    _this.modified_block_ids = {};
                    result = true;
                },
                function (state, data) {
                    result = false;
                });
        }
        this.modified_block_ids = {}
    };

    this.save_block_pos = function() {
        var ord_list = new Array();
        block_items = $(this.content).find('.block-item');
        for (var i = 0; i < block_items.length; i += 1) {
            ord_list.push(parseInt($(block_items[i]).attr('id').toString().split('_')[0]));
        }
        save_block_pos_do('[' + ord_list.toString() + ']',
            function (state, data) {
                notify('Save block position...done');    
            },
            function (state, data) {
                notify('Save block position failed!.')
            });
    },

    this.save_block_states = function() {
        var arr = [];
        for (var id in this.blocks) {
            var pair = []
            pair.push(id);
            pair.push(this.blocks[id].is_fold);
            arr.push('['+pair.toString() + ']');
        }
        save_block_states_do('[' + arr.toString() + ']',
            function (state, data) {
                notify('Save block state...done');    
            },
            function (state, data) {
                notify('Save block state failed!.')
            });
    },

    this.update_block_list = function () {
        var _this = this;
        get_block_list_do (this.id, 
        function (state, data) {
            var bs = data;
            for (var i = 0; i < bs.length; i += 1) {
                var block = new ubi.block(bs[i], _this);
                $(_this.content).find('.block-item-ul').append(
                    block.html()
                );
                block.bind_events();
                block.update_tags();
                block.init();
                _this.blocks[block.id] = block;
            }
            //actions
            _this.block_items_dragsort_bind();
        },
        function (state, raw) {
            notify('Load block list fail');  
        });
    };

    this.block_items_dragsort_bind = function() {
        $('#clip-frame-ul, ' + this.div + ' .block-item-ul').dragsort(
            { dragSelector: '.block-item-drag-handler, .clip-item-drag-handler', 
            /* NEED TO MORE WORK HERE.*/
            dragBegin: function(event) {

            },
            dragEnd: function(event) {

            }, 
            dragBetween: true 
        });
    }

    this.apply_filter = function(filter_text) {
        $(this.content).find('.block-item').each(function () {
            if ($(this).find('.block-item-content').text().search(filter_text) == -1) {
                $(this).fadeOut();
            } else {
                $(this).fadeIn();
            }                
        })
    }
    this.init();
};

